# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api


class Users(models.Model):
    _inherit = "res.users"

    related_gateway_ids = fields.One2many(
        'iot.gateway', 'related_user_id', string='Related Gateways')
    related_gateway_id = fields.Many2one(
        'iot.gateway', 'Related Gateway',
        compute='_compute_related_gateway_id',
        inverse='_inverse_related_gateway_id',
        # store=True
    )

    def _compute_related_gateway_id(self):
        for rec in self:
            rec.related_gateway_id = self.env['iot.gateway'].search(
                [('id', 'in', rec.related_gateway_ids.ids)], limit=1)

    def _inverse_related_gateway_id(self):
        for rec in self:
            rec.related_gateway_ids = rec.related_gateway_id

    @api.model
    def format_mail(self, code, *args, **kwargs):
        # 用于 继承, 重写, 定义 邮件的格式
        print('in iot, format_mail', code)

        return code
